package com.yoomiito.app.ui.yijiayou

import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.model.GasFormat
import com.yoomiito.app.model.ViewType
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.ResUtil

class GasFormatAdapter(val context: Context,var data:List<ViewType<GasFormat>>) : androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>(){

    companion object{
        const val Subtitle = 1
        const val Content = 2
    }

    var onClickListener:(GasFormat)->Unit={}

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        var vh: androidx.recyclerview.widget.RecyclerView.ViewHolder?=null
        when (p1) {
            Subtitle -> {
                val view = LayoutInflater.from(context).inflate(R.layout.item_tv,p0,false)
                vh = SubtitleViewHolder(view)
            }
            Content -> {
                val view = LayoutInflater.from(context).inflate(R.layout.item_tv,p0,false)
                vh = ContentViewHolder(view)
            }
        }
        return vh!!
    }

    override fun getItemCount(): Int = data.size

    override fun onBindViewHolder(p0: androidx.recyclerview.widget.RecyclerView.ViewHolder, p1: Int) {
        val item = data[p1].t
        when(p0){
            is SubtitleViewHolder->{
                p0.itemTv.let {
                    it.text = item.name
                    it.paint.isFakeBoldText = true
                    it.setTextColor(ResUtil.getColor(R.color.color_balk_444444))
                    it.setBackgroundResource(R.color.color_white)
                    it.setPadding(0,0,0,0)
                }

            }
            is ContentViewHolder->{
                p0.itemTv.let {
                    it.text = item.name
                    val lp = it.layoutParams as androidx.recyclerview.widget.RecyclerView.LayoutParams
                    lp.width = DensityUtil.dp2px(78f)
                    lp.height = DensityUtil.dp2px(30f)
                    it.gravity = Gravity.CENTER
                    it.setPadding(0,0,0,0)
                    it.background = if (item.select)  ResUtil.getDrawable(R.drawable.bg_color_3bb033_15) else ResUtil.getDrawable(R.drawable.bg_flowlayout)
                    it.setOnClickListener {
                        onClickListener(item)
                        data.forEach {i->
                            if (i.t.select) {
                                i.t.select = false
                            }
                        }
                        item.select = true
                        notifyDataSetChanged()
                    }
                }
            }
        }
    }

    override fun getItemViewType(position: Int): Int = data[position].viewType

    inner class SubtitleViewHolder(view: View): androidx.recyclerview.widget.RecyclerView.ViewHolder(view){
        val itemTv: TextView = view.findViewById(R.id.item_tv)
    }

    inner class ContentViewHolder(view: View): androidx.recyclerview.widget.RecyclerView.ViewHolder(view){
        val itemTv: TextView = view.findViewById(R.id.item_tv)
    }

}